/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client;

import net.bioteam.appweb.gwt.apps.client.Util.DumpBooleanCallback;
import net.bioteam.appweb.gwt.apps.client.Util.ModalWindow;
import net.bioteam.appweb.gwt.apps.client.dataanalysis.DataInputPanel;
import net.bioteam.appweb.gwt.apps.client.dataanalysis.DataProgramForm;
import net.bioteam.appweb.gwt.apps.client.dataanalysis.DataProgramFormPanel;
import net.bioteam.appweb.gwt.apps.client.dataanalysis.ServicePanel;
import net.bioteam.appweb.gwt.apps.client.dataanalysis.ServicesGrid;
import net.bioteam.appweb.gwt.apps.client.datatree.DataTreeGrid;
import net.bioteam.appweb.gwt.apps.client.datatree.UploadWindow;
import net.bioteam.appweb.gwt.apps.client.ds.DatasetPanel;
import net.bioteam.appweb.gwt.apps.client.program.AppTreeGrid;
import net.bioteam.appweb.gwt.apps.client.program.ProgramForm;
import net.bioteam.appweb.gwt.apps.client.program.ProgramFormHolder;
import net.bioteam.appweb.gwt.apps.client.program.ServerFileWindow;
import net.bioteam.appweb.gwt.apps.client.qstat.Sgeqstat;
import net.bioteam.appweb.gwt.apps.client.user.AccountPanel;
import net.bioteam.appweb.gwt.apps.client.user.JobDetailGrid;
import net.bioteam.appweb.gwt.apps.client.user.JobGrid;
import net.bioteam.appweb.gwt.apps.client.user.JobPanel;
import net.bioteam.appweb.gwt.apps.client.user.TemplateGrid;
import net.bioteam.appweb.gwt.apps.client.user.TemplatePanel;
import net.bioteam.appweb.gwt.apps.client.user.UsersGrid;
import net.bioteam.appweb.gwt.apps.client.user.UsersPanel;
import net.bioteam.common.user.User;

import com.google.gwt.user.client.Timer;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.widgets.Label;

public class StaticHolder
{
	/**
	 * Class that hold the instances that exists only once in the application.
	 * All those instances can be referred here so that they can be accessed
	 * easily.
	 */
	public static LoginHolder loginPanel;
	public static MainPanel mainPanel;
	public static AppTreeGrid appTreeGrid;
	public static ContentPanel content;
	public static ProgramForm programForm;
	public static JobGrid jobGrid;
	public static JobDetailGrid jobDetailGrid;
	public static DumpBooleanCallback dumpB = new DumpBooleanCallback();
	public static ProgramFormHolder programFormHodler;

	private static UsersGrid usersGrid;
	private static UsersPanel usersPanel;

	private static DataTreeGrid dataTreeGrid;
	public static User user;

	public static JobPanel jobPanel;
	public static ModalWindow loadingWindow;

	public static void init()
	{
		loginPanel = new LoginHolder();
		mainPanel = new MainPanel();
		loadingWindow = new ModalWindow(mainPanel);

	}

	public static UsersPanel getUsersPanel()
	{
		if (usersPanel == null)
		{
			usersPanel = new UsersPanel();
			usersPanel.setVisible(false);
			content.addChild(usersPanel);
		}
		return usersPanel;
	}

	public static DataTreeGrid getDataTreeGrid()
	{
		if (dataTreeGrid == null)
		{
			dataTreeGrid = new DataTreeGrid();
			// dataTreeGrid.setVisible(false);
			// content.addChild(dataTreeGrid);
		}
		return dataTreeGrid;
	}

	public static UsersGrid getUsersGrid()
	{
		getUsersPanel();
		return usersGrid;
	}

	public static void setUsersGrid(UsersGrid u)
	{
		usersGrid = u;
	}

	private static UploadWindow uploadWindow;

	public static UploadWindow getUploadWindow()
	{
		if (uploadWindow == null)
		{
			uploadWindow = new UploadWindow();
		}
		return uploadWindow;
	}

	private static ServerFileWindow serverFileWindow;

	public static ServerFileWindow getServerFileWindow()
	{
		if (serverFileWindow == null)
			serverFileWindow = new ServerFileWindow();
		return serverFileWindow;
	}

	public static AccountPanel accountPanel;

	public static TemplateGrid templateGrid;

	public static TemplateGrid getTemplateGrid()
	{
		return templateGrid;
	}

	private static TemplatePanel templatePanel;

	public static TemplatePanel getTemplatePanel()
	{
		if (templatePanel == null)
		{
			templatePanel = new TemplatePanel();
			templatePanel.setVisible(false);
			content.addChild(templatePanel);
		}
		return templatePanel;
	}

	private static DataInputPanel dataInputPanel;

	public static DataInputPanel getDataInputPanel()
	{
		if (dataInputPanel == null)
		{
			dataInputPanel = new DataInputPanel();
			dataInputPanel.setVisible(false);
			content.addChild(dataInputPanel);
		}
		return dataInputPanel;
	}

	private static ServicePanel servicePanel;

	public static ServicePanel getServicePanel()
	{
		if (servicePanel == null)
		{
			servicePanel = new ServicePanel();
			servicePanel.setVisible(false);
			content.addChild(servicePanel);
		}
		return servicePanel;
	}

	public static ServicesGrid servicesGrid;

	public static DataProgramForm dataProgramForm;
	private static DataProgramFormPanel dataProgramFormPanel;

	public static DataProgramFormPanel getDataProgramFormPanel()
	{
		if (dataProgramFormPanel == null)
		{
			dataProgramFormPanel = new DataProgramFormPanel();
			dataProgramFormPanel.setVisible(false);
			content.addChild(dataProgramFormPanel);
		}
		return dataProgramFormPanel;
	}

	static Label statusLabel;

	public static void setStatusMessage(String message)
	{
		setStatusMessage(message, 5);
	}

	public static void setStatusMessage(String message, int seconds)
	{
		if (message != null)
		{

			statusLabel.setContents(message);
			statusLabel.animateShow(AnimationEffect.FADE);
			Timer t = new Timer()
			{

				@Override
				public void run()
				{
					statusLabel.animateHide(AnimationEffect.FADE);

				}

			};
			t.schedule(seconds * 1000);
		} else
		{
			statusLabel.hide();
		}
	}

	public static void setMessage(final Label label, String message, int seconds)
	{

		label.setContents(message);
		label.animateShow(AnimationEffect.FADE);
		Timer t = new Timer()
		{

			@Override
			public void run()
			{
				label.animateHide(AnimationEffect.FADE);

			}

		};
		t.schedule(seconds * 1000);

	}

	private static ReloginWindow reloginWindow;

	public static ReloginWindow getReloginWindow()
	{
		if (reloginWindow == null)
		{
			reloginWindow = new ReloginWindow();
			reloginWindow.setVisible(false);
		}
		return reloginWindow;
	}

	public static DatasetPanel datasetPanel;
	
	private static Sgeqstat qstat;
	public static Sgeqstat getQstat()
	{
		if (qstat==null)
		{
			qstat=new Sgeqstat();
			content.addChild(qstat);
		}
		return qstat;
	}
}
